package core;

public class TransferItemCommand extends Command
{
	
	private ProductContainer targetPC;
	private ProductContainer fromPC;
	private StorageUnit targetSU;
	private StorageUnit fromSU;
	private Item item;
	private boolean addedProduct;
	private CoreFacade core;
	private ProductManager productManager;
	private ItemManager itemManager;
	
	/**
     * Transfers items from original product container to a new one, that was
     * specified by the user
     * @param item unique item to be transferred
     * @param targetPC product container to which the item should be moved
     * @param targetSU storage unit in which the item is being moved to.
     */
	public TransferItemCommand(Item item, ProductContainer targetPC,  
			 StorageUnit targetSU)
	{
		super();
		core = CoreFacade.getInstance();
		productManager = core.getPM();
		itemManager = core.getIM();
		this.item = item;
//		this.itemCopy = new Item()
		this.targetPC = targetPC;
		this.targetSU = targetSU;
		this.fromPC = item.getProductContainer();
		this.fromSU = core.getParentStorageUnit(this.fromPC);
		addedProduct = false;
	}
	@Override
	public Item execute()
	{
		try{
			
			Item itemToMove = item;
			ProductContainer pc = productManager.getContainer(targetSU, itemToMove.getProduct());
	    	if(pc == null) //Product was not found in target storageUnit
	    	{
	    		productManager.addProductToContainer(itemToMove.getProduct(), targetPC);
    			addedProduct = true;
	    		itemManager.addItemToContainer(itemToMove, targetPC);
	    	}
	    	else //Product was found in storageUnit
	    	{
	    		if(targetPC.equals(pc)) //product is already in targetPC
	    		{
	    			itemManager.addItemToContainer(itemToMove, targetPC);
	    			addedProduct = false;
	    		}
	    		else //Product was in storage unit but in another product container
	    		{
	    			//core.transferProduct(itemToMove.getProduct(), pc, targetPC);
	    			//addedProduct = true;
	    			// add item to where all the other items are
	    			itemManager.addItemToContainer(itemToMove, pc);
	    		}
	    	}
	        core.sendNotify(itemToMove);
			
			return item;
		}
		catch (Exception e)
		{
			core.sendNotify("TransferItemCommand failed:: "+ e.getClass()+" "+ e.getMessage()  );
			addedProduct = false;
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Item undo()
	{
		try{
			
			Item itemToMove = item;
			ProductContainer pc = productManager.getContainer(fromSU, itemToMove.getProduct());
	    	if(pc == null) //Product was not found in target storageUnit
	    	{
	    		// should never hit this code
	    		productManager.addProductToContainer(itemToMove.getProduct(), fromPC);
    			//addedProduct = true;
	    		itemManager.addItemToContainer(itemToMove, fromPC);
	    	}
	    	else //Product was found in storageUnit
	    	{
	    		if(fromPC.equals(pc)) //product is already in targetPC
	    		{
	    			itemManager.addItemToContainer(itemToMove, fromPC);
//	    			addedProduct = false;
	    		}
	    		else //Product was in storage unit but in another product container
	    		{
	    			// should never hit this code
	    			core.sendNotify("Error in TransferItemCommand: undo");
	    			core.transferProduct(itemToMove.getProduct(), pc, fromPC);
//	    			addedProduct = true;
	    			itemManager.addItemToContainer(itemToMove, fromPC);
	    		}
	    	}
	    	if(addedProduct)
	    	{
		    	productManager.deleteProductFromContainer(itemToMove.getProduct(), targetPC);
		    	addedProduct = false;
	    	}
	    	
	        core.sendNotify(itemToMove);
			
			return item;
		}
		catch (Exception e)
		{
			core.sendNotify("TransferItemCommand failed:: "+ e.getClass()+" "+ e.getMessage()  );
			addedProduct = false;
			e.printStackTrace();
			return null;
		}
	}


}
